package com.uenpay.im.panel.more;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qyx.android.weight.view.MyGridView;
import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class MoreAdapter extends PagerAdapter {
	private final int ONE_PAGE_COUNT = 8;
	private LayoutInflater _inflater;
	private Context _context;
	private IMoreOnClickListener mIMoreOnClickListener;
	private ArrayList<MoreEntity> arrayList;

	public MoreAdapter(Context context, ArrayList<MoreEntity> _ArrayList,
			IMoreOnClickListener _IMoreOnClickListener) {
		arrayList = _ArrayList;
		_context = context;
		_inflater = LayoutInflater.from(context);
		mIMoreOnClickListener = _IMoreOnClickListener;
	}

	@Override
	public int getCount() {
		return (arrayList.size() - 1 + ONE_PAGE_COUNT) / ONE_PAGE_COUNT;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ViewPager pViewPager = ((ViewPager) container);
		ArrayList<MoreEntity> selectedList = new ArrayList<MoreEntity>();
		int nextIndex = Math.abs(Math.min(arrayList.size()
				- (ONE_PAGE_COUNT * position), ONE_PAGE_COUNT));
		for (int i = position * ONE_PAGE_COUNT; i < position * ONE_PAGE_COUNT
				+ nextIndex; i++) {
			// if (msg_list.get(i) != null)
			selectedList.add(arrayList.get(i));
		}

		View view = _inflater.inflate(R.layout.gif_grid, container,false);
		MyGridView gridView = (MyGridView) view
				.findViewById(R.id.yutumei_gridview);
		gridView.setAdapter(new MoreGridAdapter(_context, selectedList,
				mIMoreOnClickListener));
		pViewPager.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup collection, int position, Object view) {
		((ViewPager) collection).removeView((View) view);
	}

}
